package com.util;

import com.alibaba.fastjson.JSON;
import com.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

public abstract class BaseServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            resp.setCharacterEncoding("utf-8");
            req.setCharacterEncoding("utf-8");
            String methodName = req.getParameter("type");
            //String requestURI = req.getRequestURI();
            Class<?> servletClass = getServletClass();
            Object o = servletClass.newInstance();

            Method method = servletClass.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

            Object invoke = method.invoke(o, req, resp);
            if(invoke instanceof String){
                String jspName=invoke.toString();
                req.getRequestDispatcher("admin/page/"+jspName+".jsp").forward(req,resp);
            }else if(invoke instanceof ResultData){
                ResultData resultData=(ResultData)invoke;
                //利用alibaba fastJson
                String string = JSON.toJSONString(resultData);
                //利用response将json以流的方式写入请求头信息
                PrintWriter printWriter = resp.getWriter();
                printWriter.write(string);
                printWriter.flush();
                printWriter.close();
            }else if(invoke instanceof IninJson){
                IninJson ininJson=(IninJson)invoke;
                //利用alibaba fastJson
                String string = JSON.toJSONString(ininJson);
                //利用response将json以流的方式写入请求头信息
                 PrintWriter printWriter = resp.getWriter();
                printWriter.write(string);
                printWriter.flush();
                printWriter.close();
            }else if(invoke instanceof LayuiTable){
                LayuiTable layuiTable=(LayuiTable)invoke;
                //利用alibaba fastJson
                String string = JSON.toJSONString(layuiTable);
                //利用response将json以流的方式写入请求头信息
                PrintWriter printWriter = resp.getWriter();
                printWriter.write(string);
                printWriter.flush();
                printWriter.close();
            }else if(invoke instanceof List){
                List layuiTable=(List)invoke;
                //利用alibaba fastJson
                String string = JSON.toJSONString(layuiTable);
                //利用response将json以流的方式写入请求头信息
                PrintWriter printWriter = resp.getWriter();
                printWriter.write(string);
                printWriter.flush();
                printWriter.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public abstract Class<?> getServletClass();
}
